<!DOCTYPE html>
<!--
  ~ Copyright 2018 ThoughtWorks, Inc.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<html>
<head>
  <title>GoCD server - Loading ...</title>
  <meta charset="utf-8">
  <style type="text/css">
    body {
      margin: 0 auto;
    }

    #logo {
      width: 120px;
      margin: 2em auto;
    }

    #logo svg .st0 {
      fill: black;
    }

    .section-by-status {
      text-align: center;
      font-size: 24px;
      font-family: sans-serif, serif;
      width: 50%;
      margin: 0 auto;
    }

    .section-by-status a {
      text-decoration: none;
    }

    #update-check-message {
      text-align: center;
      font-size: small;
      margin-top: 2em;
    }
  </style>
</head>

<body>

<div id="logo">
  <!-- Copy of: server/src/main/webapp/WEB-INF/rails/app/assets/images/go_logo.svg -->
  <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 viewBox="32.6 578.5 654.2 324.8" style="enable-background:new 32.6 578.5 654.2 324.8;" xml:space="preserve">
<style type="text/css">
	.st0{fill:#FFFFFF;}
</style>
<g>
	<path class="st0" d="M481.9,848.7c-4.2,11.3-10.2,21-18,29.1s-17.1,14.4-28,18.8c-10.9,4.4-22.9,6.7-36,6.7
		c-20.4,0-37.9-5.1-52.4-15.2c-14.5-10.1-24-23.9-28.5-41.5h29.6c2.9,6.4,6.5,11.6,11,15.5c4.4,3.9,9.2,6.8,14.2,8.8s9.8,3.3,14.5,4
		s8.5,1,11.7,1c10.9,0,20.1-1.9,27.8-5.8c7.7-3.9,13.9-8.9,18.6-15c4.8-6.1,8.2-13,10.3-20.8c2.1-7.8,3.2-15.5,3.2-23.3v-18
		c-6.2,8.4-14.4,15.6-24.5,21.5c-10.1,5.9-21.4,8.8-33.8,8.8s-23.9-2.3-34.3-7s-19.5-11.1-27.1-19.3c-7.7-8.2-13.7-17.8-18-28.6
		c-4.3-10.9-6.5-22.5-6.5-35s2.2-24.1,6.5-35.1s10.3-20.5,18-28.6c7.7-8.1,16.7-14.5,27.1-19.3s21.9-7.2,34.3-7.2
		c12.7,0,24.2,3.1,34.6,9.3c10.4,6.2,18.6,13.7,24.6,22.3v-27.3h27.3V811C488.2,824.9,486.1,837.4,481.9,848.7z M455.3,708.9
		c-3.1-7.8-7.3-14.6-12.7-20.5c-5.3-5.9-11.5-10.5-18.5-13.8s-14.5-5-22.5-5s-15.5,1.7-22.6,5s-13.2,7.9-18.3,13.8
		c-5.1,5.9-9.2,12.7-12.2,20.5s-4.5,16.1-4.5,25c0,8.7,1.5,16.9,4.5,24.6c3,7.8,7,14.5,12.2,20.3c5.1,5.8,11.2,10.3,18.3,13.7
		c7.1,3.3,14.7,5,22.6,5c8,0,15.5-1.7,22.5-5s13.2-7.9,18.5-13.7s9.5-12.5,12.7-20.3c3.1-7.8,4.7-16,4.7-24.6
		C459.9,725,458.4,716.6,455.3,708.9z"/>
	<path class="st0" d="M628.6,650.4c10.5,4.8,19.6,11.3,27.3,19.5s13.7,17.8,18.1,28.8c4.4,11,6.7,22.7,6.7,35.1
		c0,12.4-2.2,24-6.7,34.8c-4.4,10.8-10.5,20.2-18.1,28.3c-7.7,8.1-16.8,14.5-27.3,19.1c-10.5,4.6-21.9,7-34.1,7
		c-12.2,0-23.5-2.3-34-7s-19.5-11-27.3-19.1c-7.8-8.1-13.9-17.5-18.3-28.3c-4.4-10.8-6.7-22.4-6.7-34.8c0-12.4,2.2-24.1,6.7-35.1
		c4.4-11,10.5-20.6,18.3-28.8s16.9-14.7,27.3-19.5c10.4-4.8,21.8-7.2,34-7.2C606.7,643.3,618,645.7,628.6,650.4z M616.9,792.1
		c7-3.3,13.2-7.8,18.5-13.5s9.5-12.4,12.5-20.1c3-7.8,4.5-16,4.5-24.6c0-8.9-1.5-17.3-4.5-25.1c-3-7.9-7.2-14.7-12.5-20.5
		s-11.5-10.4-18.5-13.8c-7-3.4-14.5-5.2-22.5-5.2s-15.5,1.7-22.5,5.2c-7,3.4-13.1,8-18.3,13.8s-9.3,12.6-12.3,20.5
		c-3,7.9-4.5,16.3-4.5,25.1c0,8.7,1.5,16.9,4.5,24.6c3,7.8,7.1,14.5,12.3,20.1c5.2,5.7,11.3,10.2,18.3,13.5s14.5,5,22.5,5
		C602.5,797.1,609.9,795.4,616.9,792.1z"/>
</g>
<path class="st0" d="M61,861.9c-4.6,0-9.2-1.1-13.4-3.4c-9.2-4.9-15-14.5-15-25V720.2c0-15.7,12.7-28.3,28.3-28.3
	s28.3,12.7,28.3,28.3v60.4l90.6-60.4L45.3,630.4c-13-8.7-16.5-26.3-7.9-39.3c8.7-13,26.3-16.5,39.3-7.9l170.1,113.4
	c7.9,5.3,12.6,14.1,12.6,23.6s-4.7,18.3-12.6,23.6l-170,113.4C72,860.3,66.5,861.9,61,861.9z"/>
<g>
	<path class="st0" d="M685.7,612.6c-0.8,1.8-1.8,3.3-3.1,4.6c-1.3,1.3-2.8,2.3-4.6,3.1c-1.8,0.8-3.6,1.1-5.6,1.1s-3.9-0.4-5.6-1.1
		s-3.3-1.8-4.6-3.1c-1.3-1.3-2.3-2.8-3.1-4.6S658,609,658,607s0.4-3.9,1.1-5.6c0.8-1.7,1.8-3.3,3.1-4.6c1.3-1.3,2.8-2.3,4.6-3.1
		c1.8-0.8,3.6-1.1,5.6-1.1s3.9,0.4,5.6,1.1s3.3,1.8,4.6,3.1c1.3,1.3,2.3,2.8,3.1,4.6c0.8,1.7,1.1,3.6,1.1,5.6
		C686.8,609,686.4,610.9,685.7,612.6z M683,602.4c-0.6-1.4-1.4-2.7-2.5-3.7c-1-1.1-2.3-1.9-3.7-2.5c-1.4-0.6-2.9-0.9-4.5-0.9
		s-3.1,0.3-4.5,0.9c-1.4,0.6-2.6,1.5-3.7,2.5c-1,1.1-1.9,2.3-2.5,3.7s-0.9,3-0.9,4.6c0,1.6,0.3,3.2,0.9,4.6c0.6,1.4,1.4,2.7,2.5,3.8
		c1,1.1,2.3,1.9,3.7,2.5c1.4,0.6,2.9,0.9,4.5,0.9s3.1-0.3,4.5-0.9c1.4-0.6,2.6-1.5,3.7-2.5c1-1.1,1.9-2.3,2.5-3.8
		c0.6-1.4,0.9-3,0.9-4.6C683.9,605.4,683.6,603.8,683,602.4z M676.2,614.6l-2.9-5.6h-3.9v5.5h-3v-15.9h7.6c1.7,0,3,0.5,4,1.4
		c1,0.9,1.5,2.2,1.5,3.8c0,1.1-0.3,2.1-0.9,3c-0.6,0.9-1.4,1.5-2.4,1.8l3.4,5.9h-3.4V614.6z M674.1,606.3c0.6,0,1.2-0.2,1.7-0.5
		c0.5-0.4,0.8-1,0.8-1.9c0-0.9-0.2-1.5-0.7-1.9c-0.4-0.4-1-0.5-1.8-0.5h-4.7v4.9L674.1,606.3L674.1,606.3z"/>
</g>
</svg>
</div>

<div class="section-by-status" id="status-STARTING">
  GoCD server is starting. Please wait ...
</div>

<div class="section-by-status" id="status-STARTED" style="display: none;">
  GoCD server has started. You will be automatically redirected to it in a few seconds. <a href="#" onclick="window.location.reload(true);">Go directly to the GoCD server.</a>
</div>

<div class="section-by-status" id="status-UNKNOWN" style="display: none;">
  Unable to contact the GoCD server. It might not have started. Please check the server logs.
</div>

<div id="update-check-message"></div>
</body>

<script type="text/javascript">
  // https://github.com/yanatan16/nanoajax/blob/cc89609d8f4a17a346b27e2f85c18e14de18a76b/nanoajax.min.js
  !function(t,e){function n(t){return t&&e.XDomainRequest&&!/MSIE 1/.test(navigator.userAgent)?new XDomainRequest:e.XMLHttpRequest?new XMLHttpRequest:void 0}function o(t,e,n){t[e]=t[e]||n}var r=["responseType","withCredentials","timeout","onprogress"];t.ajax=function(t,a){function s(t,e){return function(){c||(a(void 0===f.status?t:f.status,0===f.status?"Error":f.response||f.responseText||e,f),c=!0)}}var u=t.headers||{},i=t.body,d=t.method||(i?"POST":"GET"),c=!1,f=n(t.cors);f.open(d,t.url,!0);var l=f.onload=s(200);f.onreadystatechange=function(){4===f.readyState&&l()},f.onerror=s(null,"Error"),f.ontimeout=s(null,"Timeout"),f.onabort=s(null,"Abort"),i&&(o(u,"X-Requested-With","XMLHttpRequest"),e.FormData&&i instanceof e.FormData||o(u,"Content-Type","application/x-www-form-urlencoded"));for(var p,m=0,v=r.length;v>m;m++)p=r[m],void 0!==t[p]&&(f[p]=t[p]);for(var p in u)f.setRequestHeader(p,u[p]);return f.send(i),f},e.nanoajax=t}({},function(){return this}());
</script>

<script type="text/javascript">

  var intervalSizeInSeconds = 2;
  var numberOfIntervalsBetweenStatusUpdateCalls = 4;

  var hide = function hide(element) {
    element.style = "display: none";
  };

  var show = function show(element) {
    element.style = "display: inherit";
  };

  var updateMessage = function updateMessage(message) {
    document.getElementById("update-check-message").textContent = message;
  };

  var handleResponse = function handleResponse(status) {
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
      var elements = document.getElementsByClassName("section-by-status");
      for (var i = 0; i < elements.length; i++) {
        hide(elements[i]);
      }
    } catch (err) {
      _didIteratorError = true;
      _iteratorError = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion && _iterator.return) {
          _iterator.return();
        }
      } finally {
        if (_didIteratorError) {
          throw _iteratorError;
        }
      }
    }

    show(document.getElementById("status-" + status));
  };

  var getStatus = function getStatus(callbackFn) {
    window.nanoajax.ajax({
      url: '/go/api/v1/health'
    }, function (code, responseText) {
      var status = "UNKNOWN";
      if (code === 200) {
        status = "STARTED";
      } else if (code === 503) {
        status = "STARTING";
      }

      callbackFn(status);
    });
  };

  var incrementingIndex = function () {
    var i = 0;
    return function () {
      return ++i;
    };
  }();

  var timer = setInterval(function () {
    var index = incrementingIndex();
    var nextCheck = (numberOfIntervalsBetweenStatusUpdateCalls - index % numberOfIntervalsBetweenStatusUpdateCalls) * intervalSizeInSeconds;

    updateMessage("(Next check in " + nextCheck + " seconds)");

    if (index % numberOfIntervalsBetweenStatusUpdateCalls !== 0) {
      return;
    }

    updateMessage("Checking ...");
    getStatus(function (status) {
      if (status === "STARTED") {
        updateMessage("");
        clearInterval(timer);
        setTimeout(function () {
          return window.location.reload(true);
        }, 4000);
      }
      handleResponse(status);
    });
  }, intervalSizeInSeconds * 1000);

</script>
</html>
